<?php
class Get_model extends Model {

    function Get_model()
    {
        parent::Model();
        $this->load->database();
    }

    /**
     * get user item list
     *
     * gets the list of items submitted to the database by a given user.
     * matches the user based on the logged in user id.
     *
     * @param int $id
     * @return array
     */
    function get_useritemlist($id)//take in $userid
    {
        //create array for list $results
        $Q = "SELECT item.Image, item.Note, item.IID FROM item WHERE item.UID = $id";
        //create query - select name,note,date, FROM item Where userid = $user
        $result = $this->db->query($Q);
        return $result->result();
        //return $results
    }

    /**
     * get user list
     *
     * returns a list of all non admin users
     *
     * @return array
     */
    function get_userlist()
    {
        //create array $results
        $Q = 'SELECT user.Name, user.Email, user.UID
                FROM user
                WHERE user.Permission = 0;';
        $result = $this->db->query($Q);
        return $result->result();
        //return $result
    }

    /**
     *  get item list
     *
     * returns a list of all items in the database
     *
     * @return array
     */
    function get_itemlist()//$id - userid
    {
        $Q = 'SELECT user.Name, item.Image, item.Note, item.IID
            FROM item INNER JOIN user ON user.UID = ITEM.UID
            ORDER BY item.IID;';

        $result = $this->db->query($Q);
        //$result = $this->db->get('user');
        return $result->result();
        //return $result
    }

    function get_vote()
    {
        //array $results
        //query - SELECT COUNT(vote.itemid),item.itemid SORTBY COUNT()
        //return $results
    }

    /**
     * get random image
     *
     * gets a list of all items from the database and takes two at random
     * to return
     *
     * @return array
     */
    function get_rimage()
    {
        
        $Q = "SELECT item.IID, item.Image, item.Note FROM item;";
        $result = $this->db->query($Q);
        $results = $result->result();
        $ir1 = rand(0, count($results)-1);
        $ir2 = rand(0, count($results)-1);
            while($ir1==$ir2)
                {
                    $ir2 = rand(0, count($results)-1);
                }
        $image = array();
        $image['0'] = $results["$ir1"];
        $image['1'] = $results["$ir2"];
        return $image;
        //return $result->result();
        

    }

    /**
     * get image results
     *
     * used to return the top voted items. returns the top 10
     *
     * @return array
     */
    function get_imageresult()
    {
        $Q = 'SELECT item.Image, item.Note, vote.VID, COUNT(item.Image)
                FROM item INNER JOIN vote ON item.IID = vote.IID
                GROUP BY item.Image
                ORDER BY COUNT(item.Image) desc;';
        $result = $this->db->query($Q);
        $results = $result->result();

        $image = array();
        $image['0'] = $results["0"];
        $image['1'] = $results["1"];
        $image['2'] = $results["2"];
        $image['3'] = $results["3"];
        $image['4'] = $results["4"];
        $image['5'] = $results["5"];
        $image['6'] = $results["6"];
        $image['7'] = $results["7"];
        $image['8'] = $results["8"];
        $image['9'] = $results["9"];
        return $image;
    }

    

 }
//EOF
